import tensorflow as tf
from tensorflow import keras
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from self_mudle.plot_learning_curves import plot_learning_curves

dataset = fetch_california_housing()
train_x, test_x, train_y, test_y = train_test_split(dataset.data,dataset.target,test_size=0.2,random_state=42)
train_x, valid_x, train_y, valid_y = train_test_split(train_x,train_y,test_size=0.2,random_state=42)


scaler = StandardScaler()
train_x = scaler.fit_transform(train_x)
valid_x = scaler.transform(valid_x)
test_x = scaler.transform(test_x)

# wide_deep模型 https://www.cnblogs.com/yinzm/p/11878831.html
# 函数式API最后要生成模型 keras.Squential()是自动生成模型
# keras.models.Model 生成是要inputs 和 outputs 两个参数
input = keras.layers.Input(shape=train_x.shape[1:])
hidden1 = keras.layers.Dense(30,activation='relu')(input)
hidden2 = keras.layers.Dense(30,activation='relu')(hidden1)

concat = keras.layers.concatenate([input,hidden2])  # 合并wide(None,8)和deep(None,30) -> (None,38) 参数默认axis=-1
output = keras.layers.Dense(1)(concat)

model = keras.models.Model(inputs = input, outputs = output)  # 生成模型

model.summary()

model.compile( 
    optimizer = keras.optimizers.Adam(0.001),
    loss = keras.losses.mean_squared_error
)

history = model.fit(train_x, train_y, epochs=10, validation_data = (valid_x, valid_y))
model.evaluate(test_x,test_y)
# plot_learning_curves(history)
